package cn.stronglink.asset.module.basicdata.assetType.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetType;
import cn.stronglink.asset.model.Department;
import cn.stronglink.asset.module.basicdata.assetType.mapper.AssetTypeMapper;
import cn.stronglink.asset.module.basicdata.assetType.vo.AssetTypeTreeNodeVo;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class AssetTypeService {

	@Autowired
	private AssetTypeMapper assetTypeMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetTypeTreeNodeVo> getTree() {
		List<AssetTypeTreeNodeVo> rootDept = assetTypeMapper.queryRootTop();	
		List<AssetTypeTreeNodeVo> assetTypes = assetTypeMapper.queryAssetType();
		if(rootDept!=null&&rootDept.size()>0){
			for(AssetTypeTreeNodeVo vo:rootDept){
				recursion(assetTypes, vo);  
			}
		}
        return rootDept;  
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<AssetType> queryAssetTypeByPid(Page<AssetType> page,Long pid,String name) {
		page.setRecords(assetTypeMapper.queryAssetTypeByPid(page,pid,name));
		return page;	
	}
	
	public void removeAssetType(Long id){
		assetTypeMapper.removeAssetType(id);	
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Department qryDeptById(Long id){
		return assetTypeMapper.qryDeptById(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int getChildAssetTypeCount(Long id) {
		return assetTypeMapper.getChildAssetTypeCount(id);
	}  

	private void recursion(List<AssetTypeTreeNodeVo> list, AssetTypeTreeNodeVo node) {
		List<AssetTypeTreeNodeVo> childList = getChildList(list, node);// 得到子节点列表
		if (!CollectionUtils.isEmpty(childList)) {
			node.setAssetType(childList);
			Iterator<AssetTypeTreeNodeVo> it = childList.iterator();
			while (it.hasNext()) {
				AssetTypeTreeNodeVo n = (AssetTypeTreeNodeVo) it.next();
				recursion(list, n);
			}
		} else {
			node.setAssetType(null);
		}
	}
	
	private List<AssetTypeTreeNodeVo> getChildList(List<AssetTypeTreeNodeVo> list, AssetTypeTreeNodeVo node) {  
        List<AssetTypeTreeNodeVo> nodeList = new ArrayList<AssetTypeTreeNodeVo>();  
        Iterator<AssetTypeTreeNodeVo> it = list.iterator();  
        while (it.hasNext()) {  
        	AssetTypeTreeNodeVo n = (AssetTypeTreeNodeVo) it.next();  
            if (n.getPid().equals(node.getId()) ) {  
                nodeList.add(n);  
            }  
        }  
        return nodeList;  
    }

	public void insertAssetType(AssetType assetType) {
		if (assetType.getId() == null) {
			assetType.setId(IdWorker.getId());
			assetType.setCreateBy(WebUtil.getCurrentUser());
			assetType.setCreateTime(new Date());
			assetTypeMapper.insertAssetType(assetType);		
		}else{
			assetType.setUpdateBy(WebUtil.getCurrentUser());
			assetType.setUpdateTime(new Date());
			assetTypeMapper.updateAssetType(assetType);
		}	
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetType> getListAll() {
		return assetTypeMapper.getListAll();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int getIsUsed(Long id) {
		return assetTypeMapper.getIsUsed(id);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Long getTypeIdByName(String name) {
		return assetTypeMapper.getTypeIdByName(name);
	}

	public List<AssetTypeTreeNodeVo> queryAllList() {
		List<AssetTypeTreeNodeVo> rootDept = assetTypeMapper.queryRoot();	
		List<AssetTypeTreeNodeVo> assetTypes = assetTypeMapper.queryAssetType();
		if(rootDept!=null&&rootDept.size()>0){
			for(AssetTypeTreeNodeVo vo:rootDept){
				recursion(assetTypes, vo);  
			}
		}
        return rootDept;  
	}
}
